#ifndef THREADS_CLASS_HEAD
#define THREADS_CLASS_HEAD


#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <vector>
#include <list>
#include <semaphore.h>
#include <set>

enum WORKER_TYPE{
    WORKER_TYPE_ONESHOT,
    WORKER_TYPE_PERIDICAL,
    WORKER_TYPE_PERM
};

class worker{
private:
public:
    int workerType;
    // async run funcs
    virtual void run(void) = 0;
    virtual void stop(void) = 0;
    virtual ~worker(){
    }
};

struct threadP;

struct workercmp{
    bool operator()(const worker* a, const worker* b){
        return a->workerType < b->workerType;
    }    
};

class threads {
private:
    std::priority_queue<worker *,std::vector<worker *>, workercmp> tasks;
    std::set<worker *>      runningTasks;
    int                     max_thhreadNum;
    int                     numThread;
    int                     numRunningJob;
    int                     numRunningOneShot;
    int                     jobCount;
    sem_t                   queueMutex;
    sem_t                   semTasks;
    sem_t                   alljobs;
    std::vector<pthread_t>  tids;
    bool                    stopThread;

    static void * threadFunc(void *);
    bool createThread(void);
public:
    threads(void);
    threads(int _max_thhreadNum);
    void addJob(worker *);
    void addOneshot(worker *);
    void wait(void);
    // safely stop threads
    void stop(void);
};

struct threadP{
    int threadNum;
    threads * th;
};




#endif // THREADS_CLASS_HEAD
